﻿$(document).ready(function () {
    pageload();
    $("#btnadddevice").click(function () { addShow(); });
    $("#btnSuggestion").click(function () { AddSuggestion(); });
});
var methods = new GetDataJSON();
function pageload() {
    methods.GetGates(_baseModel.openid, GatesLoad);
    //provinceList = new Vue({ el: '#provinceList', data: { items: [] } });
}
var otherList = [];
var editModel = [];
var provinceList = [];
var homeaddrList = [];
function GatesLoad(result) {
    var html = '';
    if (!checkStatus(result.Status)) {
        html += "<li>" + result.Message + "</li>"
        $("#otherList").html(html);
    } else {
        for (var i = 0; i < result.Data.length; i++) {
            var model = result.Data[i];
            result.Data[i].push(methods.GetImg(model[0]));
        }
        otherList = new Vue({ el: '#otherList', data: { items: result.Data } });
        editModel = new Vue({ el: '#editdevice', data: { model: null } });
        methods.SelectProvince(function (result) {
            //provinceList = result.Data;
            var list = null;
            for (var i = 0; i < result.Data.length; i++) {
                var name = result.Data[i][0];
                var value = result.Data[i][1];
                if (list && name == list.name) {
                    list.value.push(value);
                } else {
                    if (list != null) {
                        provinceList.push(list);

                    }
                    list = { name: name, value: [value] };
                }
            }
        });
    }
}
function GatesReload(result) {
    for (var i = 0; i < result.Data.length; i++) {
        var model = result.Data[i];
        result.Data[i].push(methods.GetImg(model[0]));
    }
    otherList.items = result.Data
}
function add() {
    var txtdid = $("#adddevice_did").val();
    if (!txtdid && txtdid.length == 0) {
        alert("输入网关专享号");
        return;
    }
    //txtdid = "SIMCOM_SIM900A867330029451831";
    function callback(result) {
        if (!checkStatus(result.Status)) {
            alert(result.Message);
        } else {
            methods.GetGates(_baseModel.openid, GatesReload);
            layer.closeAll();
        }
    }
    methods.BindGate(txtdid, callback);
}
function edit() {
    function callback(result) {
        if (!checkStatus(result.Status)) {
            alert(result.Message);
        } else {
            if (editModel.model.did == _baseModel.did) {
                window.location = "/index.php?openid=o3eaus6PSrb7iEjqZ__Mtq_kuNsU&did=" + _baseModel.did + "&gatename=" + editModel.model.name;
            }
            methods.GetGates(_baseModel.openid, GatesReload);
            layer.closeAll();
        }
    }

    methods.ChangeGateInfo(editModel.model, callback);
}
function del() {
    var hiddid = $("#deletedevice_did").val();
    function callback(result) {
        if (!checkStatus(result.Status)) {
            alert(result.Message);
        } else {
            methods.GetGates(_baseModel.openid, GatesReload);
            layer.closeAll();
        }
    }
    methods.UnbindGate(hiddid, callback);
}
function addShow() {
    $("#adddevice_did").val("");
    layerShow("adddevice");
}
function editShow(_this) {
    var model = getcurrentModel(_this);
    model.provinceList = provinceList;
    model.provinceList.cityselect = function () {
        for (var i = 0; i < model.provinceList.length; i++) {
            if (model.provinceList[i].name == model.prov) {
                if (model.cityname == undefined) {
                    model.cityname = provinceList[i].value[0];
                }
                return provinceList[i].value;
            }
        }
    }
    this.editModel.model = model;
    function callback(result) {
        model.prov = result.provcn;
        model.cityname = result.namecn;
        this.editModel.model = model;
    }
    methods.GetGateStatus(model.did, callback);
    layerShow('editdevice');
}
function deleteShow(_this) {
    var model = getcurrentModel(_this);
    $("#deletedevice_name").text("确定删除" + model.name + "？");
    $("#deletedevice_did").val(model.did);
    layerShow('deletedevice');
}
function getcurrentModel(_did) {
    var model = new Object();
    var thislist = new Object();
    var list = this.otherList.items;
    for (var i = 0; i < list.length; i++) {
        if (list[i][0] == _did) {
            thislist = list[i];
            break;
        }
    }
    model.did = thislist[0];
    model.name = thislist[1];
    model.prov = thislist[2];
    model.relation = thislist[3];
    model.userimage = thislist[4];
    return model;
}

function AddSuggestion() {
    var content = $("#txtSuggestion").val();
    if (content && content.length > 0) {
        function callback(result) {
            if (!checkStatus(result.Status)) {
                alert(result.Message);
            } else {
                $("#txtSuggestion").val("");
                alert(result.Message);
            }
        }
        methods.AddSuggestion(content, callback);
    } else {
        alert("请填写您的建议内容");
    }
}
